<?php
namespace app\merchant\controller;
use app\merchant\service\UserService;
use app\common\lib\MyController;
use think\Exception;
use app\merchant\model\TpAccount;

class  Index extends MyController{
    public function index(){
        $user = UserService::get_user();
        if($this->_User_type == \app\merchant\model\User::USER_TYPE_COMPANY){
            $this->assign("real_name", $user['user_company']['company_name']);
        }
        else{
            $this->assign("real_name",$user['real_name']);
        }
        //显示的是账户余额
        //$total = $this->_Account['balance'] + $this->_Account['freeze'];//可用金额
        //商户可能充值了，充值后金额需要修改的
        $balance = TpAccount::where('user_id', '=', $this->_Uid)->value('balance');//账户余额
        //同时更新session中的值
        $this->_Account['balance'] = $balance;
        
        $this->assign("total", $balance);
        $this->assign("marketing",round($this->_Account['marketing'],2));
        $this->assign("guarantee",round($this->_Account['guarantee'],2));
        return view('Index/index');
    }
    public function home()
    {
        $this->redirect('Merchant/index');
    }



    //登录页面
    public function login(){
        return view('Login/login');
    }

    public function outLogin(){
        session('user_info',null);
        $this->redirect('index/login');
    }



    //登录操作
    public function doLogin(){
        try{
            $name = get_param('username','str');
            $password = get_param('password','str');
            $code = get_param('code','str');
            if(!captcha_check($code)){
                //throw new Exception('验证码错误');
                //因为要求验证码错误时自动刷新验证码
                return parent::returnData(99, '验证码错误');//99不要修改，这是作为验证码错误验证码错误的专用代码
            }

            $result = UserService::login($name,$password);

            if($result['code']!==0) {
                return parent::returnData(1,$result['msg']);
            }

            //注册登录信息
            $userInfo = UserService::set_user($result['data']['uid']);

            if(!session('user_info')){
                return parent::returnData(2,'userinfo is null');
            }

            if($userInfo['user_type']==2) {
                if ( empty($userInfo['user_company'])) {
                    $rs['url'] = url('user/add_company_info');
                    return parent::returnData(3,'请继续完善企业信息',$rs);
                }
                if ($userInfo['user_company']['status'] != 1) {
                    $rs['url'] = url('index/message',['title'=>base64_encode('正在审核中'),'message'=>base64_encode('您的企业材料正在人工审核中，需要1到2个工作日内完成')]);
                    return parent::returnData(0,'ok',$rs);
                }

            }
            else{
                if ( empty($userInfo['real_name'])) {
                    $rs['url'] = url('user/real_personal');
                    return parent::returnData(4,'请完成实名认证',$rs);
                }

                if ( $userInfo['user_bank'] == 0) {
                    $rs['url'] = url('user/personal_bind_card');
                    return parent::returnData(5,'请完成绑卡操作',$rs);
                }
            }


            if ($userInfo['is_set_pay_password'] != 1) {

                $rs['url'] = url('user/setpaypassword');
                return parent::returnData(6,'支付密码未设置',$rs);
            }


            if ($userInfo['merchant'] != 1) {
                $rs['url'] = url('index/message','message='.base64_encode('您的商户账户正在人工配置中，需要1到2个工作日内完成'));
                return parent::returnData(0,'商户未认证',$rs);
            }

            //设置uid存在session
            $rs['url'] = url('index/index');
            return parent::returnData(0, '登录成功',$rs);
        }
        catch (\Exception $e){
            return parent::returnData(7,'error:'.$e->getMessage());
        }

    }

    //注册
    public function register(){
        $type = input('type/d',1);
        if($type == 1){
            return view('Login/personal_register');
        }else{
            return view('Login/company_register');
        }
    }

    public function message(){
        $title = input('title','');
        $message = input('message','');
        $this->assign('title',base64_decode($title));
        $this->assign('message',base64_decode($message));
        return view('Login/message');
    }

    //注册开户
    public function doRegister(){
        try {
            $phone = get_param('phone','phone');
            $password = get_param('password','str');
            $user_type = get_param('user_type','uint',[\app\merchant\model\User::USER_TYPE_PERSON,\app\merchant\model\User::USER_TYPE_COMPANY]);
            $email = get_param('email','str','');

            //调用注册接口
            $rs = UserService::regByPhone($phone, $password, $user_type, $email);

            if ($rs['code'] == 1) {
                return parent::returnData(1, '开户失败');
            }

            //刷新用户数据缓存
            UserService::set_user($rs['data']['user_id']);

            if ($user_type == \app\merchant\model\User::USER_TYPE_PERSON) {
                $rs['url'] = url('user/real_personal');
                return parent::returnData(1, '您还未实名，请先实名认证', $rs);
            } else {
                $rs['url'] = url('user/add_company_info');
                return parent::returnData(1, '企业信息为完善请重新完善', $rs);
            }


        }
        catch (\Exception $e){
            return parent::returnData(1, $e->getMessage());
        }
    }
}

